// 防抖函数
export function debounce(func: Function, wait: number) {
  let timeout: number | null = null;
  return function(this: any, ...args: any[]) {
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(this, args);
    }, wait);
  };
}

// 检查网络状态
export function checkNetwork(): Promise<boolean> {
  return new Promise((resolve) => {
    wx.getNetworkType({
      success: (res) => {
        resolve(res.networkType !== 'none');
      },
      fail: () => resolve(false)
    });
  });
}

// 保存历史记录
export function saveHistory(record: any) {
  try {
    const history = wx.getStorageSync('qrHistory') || [];
    history.unshift(record);
    if (history.length > 10) {
      history.pop();
    }
    wx.setStorageSync('qrHistory', history);
    return true;
  } catch (e) {
    console.error('保存历史记录失败:', e);
    return false;
  }
}

// 获取历史记录
export function getHistory() {
  try {
    return wx.getStorageSync('qrHistory') || [];
  } catch (e) {
    console.error('获取历史记录失败:', e);
    return [];
  }
}

// 清空历史记录
export function clearHistory() {
  try {
    wx.removeStorageSync('qrHistory');
    return true;
  } catch (e) {
    console.error('清空历史记录失败:', e);
    return false;
  }
}